package pt.mauromad.guias.controller.admin;

import static java.lang.String.format;
import static pt.mauromad.guias.utils.transformation.Transformations.Account2Dao;

import javax.validation.constraints.NotNull;

import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.ui.Model;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import pt.mauromad.guias.bean.AccountBean;
import pt.mauromad.guias.controller.BaseController;
import pt.mauromad.guias.dao.AccountDao;
import pt.mauromad.guias.dao.DomainDao;
import pt.mauromad.guias.utils.form.Action;

public class AdminAccountController extends BaseController {
	@PreAuthorize("hasAnyRole('ROLE_ADMIN')")
	@RequestMapping(value = "/admin/account/all", method = RequestMethod.GET)
	public String list(Model model) {
		model.addAttribute("accounts", accountService.all());
		add(model, Action.createLink("new"));
		return "list/all-accounts";
	}

	@PreAuthorize("hasAnyRole('ROLE_ADMIN')")
	@RequestMapping(value = "/admin/account/{domain}", method = RequestMethod.GET)
	public String show(Model model, @PathVariable DomainDao domain) {
		model.addAttribute("account", domain.getAccount());

		if (isAdministrator())
			add(model, Action.createPost("manage"));

		add(model, Action.createLink("new.company", "company/new"));
		add(model, Action.createLink("new.user", "user/new"));

		return "account-form";
	}

	@PreAuthorize("hasAnyRole('ROLE_ADMIN')")
	@RequestMapping(value = "/admin/account/new", method = RequestMethod.GET)
	public String create(Model model) {
		model.addAttribute("domainList", domainService.available());
		model.addAttribute("editable", true);
		add(model, Action.createPost("save.new"));
		return "admin/admin-account-form";
	}

	@PreAuthorize("hasAnyRole('ROLE_ADMIN')")
	@RequestMapping(value = "/admin/account/new", method = RequestMethod.POST, params = "action=save.new")
	public String createAccount(Model model, @RequestParam @NotNull DomainDao domain) {
		AccountDao account = new AccountDao();
		account.setDomain(domain);
		accountService.save(account);
		return redirect("account/%s", domain);
	}

	@PreAuthorize("hasAnyRole('ROLE_ADMIN')")
	@RequestMapping(value = "/admin/account/{domain}", method = RequestMethod.POST, params = "action=manage")
	public String manage(Model model, @PathVariable DomainDao domain) {
		model.addAttribute("account", domain.getAccount());
		model.addAttribute("editable", true);
		model.addAttribute("accountUsers", domain.getUsers());
		add(model, Action.createPost("manage.save"));
		return "account-form";
	}

	@PreAuthorize("hasAnyRole('ROLE_ADMIN')")
	@RequestMapping(value = "/admin/account/{domain}", method = RequestMethod.POST, params = "action=manage.save")
	public String manageAccount(Model model, @PathVariable DomainDao domain, @Validated AccountBean bean) {
		AccountDao dao = Account2Dao.get(bean);
		dao.setId(domain.getAccount().getId());

		if (dao.getCompany() == null)
			dao.setEnabled(false);

		accountService.save(dao);
		return format("redirect:/account/%s", domain);
	}

	@PreAuthorize("hasAnyRole('ROLE_ADMIN')")
	@RequestMapping(value = "/admin/account/{domain}/all-users")
	public String allUsers(Model model, @PathVariable AccountDao account) {
		model.addAttribute("users", account.getUsers());
		return "list/all-users";
	}

	@PreAuthorize("hasAnyRole('ROLE_ADMIN')")
	@RequestMapping(value = "/admin/account/{domain}/all-companies")
	public String allCompanies(Model model, @PathVariable AccountDao account) {
		model.addAttribute("companies", account.getCompanies());
		return "list/all-companies";
	}
}
